from copy import deepcopy
from typing import Any, Optional

from httprunner import HttpRunner, exceptions
from httprunner.loader import load_test_file
from httprunner.parser import Parser


class HttpRunnerForBuilder(HttpRunner):
    parser: Optional[Parser] = None

    @staticmethod
    def load_testcase(data: Any) -> dict:
        # 默认的数据加载器：支持从文件和字典中加载用例数据
        if isinstance(data, dict):
            dict_data = deepcopy(data)
        elif isinstance(data, str):
            dict_data = load_test_file(data)
        else:
            raise exceptions.TestCaseFormatError(f"Invalid testcase: {data}")
        return dict_data
